package com.lpc.databindingtest2;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.lpc.databindingtest2.databinding.ActivityMainBinding;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 *lipc0113
 */
public class MainActivity extends AppCompatActivity {

    private User user;

    private String[] strings = new String[]{"onClick事件", "事件绑定"};

    private ArrayList<String> stringList = new ArrayList<>(Arrays.asList(strings));
    private Person person;
    private RecyclerView rv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ActivityMainBinding binding = DataBindingUtil.setContentView(
                this, R.layout.activity_main);

        user = new User(101, "lpc");
        binding.setUser(user);

        binding.setTime(new Date());

        binding.setIndex(1);
        binding.setStringArray(strings);
        binding.setStringList(stringList);

        binding.setTestClick(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(MainActivity.this, "onClick", Toast.LENGTH_SHORT).show();
            }
        });
        binding.setMainActivityClick(new MainActivity());

        person = new Person();
        person.name.set("ObservableField");
        person.isMan.set(true);
        binding.setPerson(person);

        initRecyclerView();
    }

    public void click1(View view) {

        user.setId(102);

//        startActivity(new Intent(this, SecondActivity.class));
    }

    public void click2(View view) {

        user.setName("lipc0113");
    }

    public void click3(View view) {

    }

    public void click4(View view) {

        Toast.makeText(MyApplication.context, "click事件绑定", Toast.LENGTH_SHORT).show();
        Log.e("lipc0113", "click事件绑定");
    }

    /**
     * TODO demo中并没有实现自定义属性
     *
     * @param view
     */
    public void click5(View view) {

    }

    public void click6(View view) {

        person.name.set("456");
    }

    private void initRecyclerView() {

        rv = findViewById(R.id.rv);

        rv.setLayoutManager(new LinearLayoutManager(this,
                LinearLayout.VERTICAL, false));

        List<User> list = new ArrayList<>();
        User user = null;
        for (int i = 0; i < 100; i++) {

            user = new User(i, "name-" + i);
            list.add(user);
        }

        rv.setAdapter(new MyRecyclerViewAdapter(this, list, R.layout.item, BR.user));
    }
}
